package entity;

import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * @author Hanlin
 */

public class DecisionLinesEvent {

	/** event id */
	private String id;
	
	/** number of rounds. */
	private int numRounds;

	/** type of event. */
	private String type;

	/** type of behavior or turn type. */
	private String behavior;

	/** question. */
	private String question;

	/** lines. */
	private ArrayList<Line> lines;

	/** user names. */
	private ArrayList<User> users;

	/** number of users. */
	private int numUser = 0;

	/** number of choice. */
	private int numChoices;

	/** number of remaining edges. */
	int remainingEdge;

	/** time information. */
	Timestamp creationDate;

	/** Who's turn it is. */
	String turn;

	String phase;
	/** final choice. */
	int choice;

	// Seems doesn't need it any more here
	// public Object[] UserNames;

	/**
	 * Constructs a new Event with the given number of rounds and numbe of
	 * choices.
	 * 
	 * @param type
	 * @param behavior
	 * @param question
	 * @param numRounds
	 * @param numChoices
	 * @param numUser
	 */
	public DecisionLinesEvent(String id, String type, String behavior, String question,
			int numRounds, int numChoices, int numUser) {
		this.id = id;
		this.setEventType(type);
		this.setTurnType(behavior);
		this.setQuestion(question);
		this.setNumRounds(numRounds);
		this.setNumChoices(numChoices);
		this.setNumUser(numUser);
		lines = new ArrayList<Line>();
		users = new ArrayList<User>();
	}

	/** Create another user into roster. */
	public void userCreate(String userName, String password) {
		users.add(new User(userName, password));
		numUser++;
	}

	/** Confirm if this edge is valid. */
	public boolean isValidEdge(Edge e) {
		for (Edge edge : getLines().get(e.left).edges) {
			if (edge.conflictWith(e)) {
				return false;
			}
		}
		return true;
	}

	/** Add an edge. */
	public boolean addEdge(Edge e) {
		if(isValidEdge(e)){
			getLines().get(e.left).addEdge(e);
			return true;
		}

		return false;
	}

	/** Set a choice. */
	public void setChoice(int position, String value) {
		if (position < 0 || position >= numChoices) {
			throw new RuntimeException("Invalid line position");
		}
		lines.add(new Line(value, position));
	}

	/** Get a choice. */
	public String getChoice(int position) {
		return lines.get(position).nameOfChoice;
	}

	/** Confirm if it is done. */
	public boolean isDone() {
		int numEdges = 0;
		for (Line l : lines){
			numEdges += l.getEdges().size();
		}
		return (this.getNumRounds() * this.getNumUser() == numEdges);
	}

	// Getters and setters
	public String getId() {
		return id;
	}
	
	public int getNumRounds() {
		return numRounds;
	}

	public void setNumRounds(int numRounds) {
		this.numRounds = numRounds;
	}

	public String getEventType() {
		return type;
	}

	public void setEventType(String type) {
		this.type = type;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public int getNumChoices() {
		return numChoices;
	}

	public void setNumChoices(int numChoices) {
		this.numChoices = numChoices;
	}

	public ArrayList<Line> getLines() {
		return lines;
	}

	public void setLines(ArrayList<Line> lines) {
		this.lines = lines;
	}

	public int getNumUser() {
		return numUser;
	}

	public void setNumUser(int numUser) {
		this.numUser = numUser;
	}

	public ArrayList<User> getUsers() {
		return users;
	}

	public void setUsers(ArrayList<User> users) {
		this.users = users;
	}

	public String getTurnType() {
		return behavior;
	}

	public void setTurnType(String behavior) {
		this.behavior = behavior;
	}

	public Timestamp getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Timestamp creationDate) {
		this.creationDate = creationDate;
	}

	public int getRemainingEdge() {
		return remainingEdge;
	}

	public void setRemainingEdge(int remainingEdge) {
		this.remainingEdge = remainingEdge;
	}

	public String getTurn() {
		return turn;
	}

	public void setTurn(String turn) {
		this.turn = turn;
	}

	public String getPhase() {
		return phase;
	}

	public void setPhase(String phase) {
		this.phase = phase;
	}
	
	public boolean removeUser(String userName) {
		int i;
		for(i=0;i<numUser;i++)
		{
			if(userName.equals(users.get(i).userName)){
				users.remove(i);
				numUser--;
				break;
			}
		}
		return true;
	}
}
